package com.css.fxfzysx.modules.vabStructure.utils;

import com.css.fxfzysx.base.exception.ParamException;
import com.css.fxfzysx.util.PlatformObjectUtils;
import org.apache.poi.ss.formula.functions.T;
import sun.font.TextRecord;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.math.BigDecimal;

public class TranslateDecimalUtils {

    public static Object translate2De(Object object,Class aclass){
        Class aClass = object.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field:fields) {
            if(PlatformObjectUtils.isNotEmpty(field.getAnnotationsByType(aclass))) {
                try {
                    field.setAccessible(true);
                    Double val = (double) field.get(object);
                    if(val.isNaN()){
                        val = 0.00;
                        continue;
                    }
                    BigDecimal bd = new BigDecimal(val);
                    val = bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                    field.set(object,val);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    throw new RuntimeException("数据转换异常！");
                }
            }

        }
        return object;
    }
    //单位转换
    public static Object unitTranslate(Object object,Class aclass){
        Class aClass = object.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field:fields) {
            if(PlatformObjectUtils.isNotEmpty(field.getAnnotationsByType(aclass))) {
                try {
                    field.setAccessible(true);
                    Double val = 0.00 ;
                    if(PlatformObjectUtils.isNotEmpty(field.get(object))){
                        val = (double) field.get(object);
                    }else {
                        continue;
                    }

                    if(val.isNaN()){
                        val = 0.00;
                        continue;
                    }
                    BigDecimal bd = new BigDecimal(val);
                    UnitTranslate  declaredAnnotation = (UnitTranslate) field.getDeclaredAnnotation(aclass);
                    int value = declaredAnnotation.value();
                    val = bd.divide(new BigDecimal(value)).doubleValue();
                    field.set(object,val);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    throw new RuntimeException("单位换算异常！");
                }
            }
        }



        return object;

    }


    //保存之前反转换
    public static Object unUnitTranslate(Object object,Class aclass){
        Class aClass = object.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field:fields) {
            if(PlatformObjectUtils.isNotEmpty(field.getAnnotationsByType(aclass))) {
                try {
                    field.setAccessible(true);
                    Double val = 0.00 ;
                    if(PlatformObjectUtils.isNotEmpty(field.get(object))){
                        val = (double) field.get(object);
                    }else {
                        continue;
                    }

                    if(val.isNaN()){
                        val = 0.00;
                        continue;
                    }
                    BigDecimal bd = new BigDecimal(val);
                    UnitTranslate  declaredAnnotation = (UnitTranslate) field.getDeclaredAnnotation(aclass);
                    int value = declaredAnnotation.value();
                    val = bd.multiply(new BigDecimal(value)).doubleValue();
                    field.set(object,val);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    throw new RuntimeException("单位换算异常！");
                }
            }
        }



        return object;

    }

}
